package com.example.etwzgzxt.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.etwzgzxt.R;
import com.example.etwzgzxt.tool.InfoApplication;
import com.example.etwzgzxt.view.activity.InfoActivity;
import com.example.etwzgzxt.view.activity.LoginActivity;


/**
 * A simple {@link Fragment} subclass.
 */
public class ProfileFragment extends Fragment {

    TextView tvUsername, tvIsLogin;

    InfoApplication info;

    public ProfileFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        info = (InfoApplication) getActivity().getApplication();
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        RelativeLayout profile = (RelativeLayout) view.findViewById(R.id.profile);
        RelativeLayout settings = (RelativeLayout) view.findViewById(R.id.settings);
        RelativeLayout about = (RelativeLayout) view.findViewById(R.id.about);
        tvUsername = (TextView) view.findViewById(R.id.username);
        tvIsLogin = (TextView) view.findViewById(R.id.isLogin);
        profile.setOnClickListener(listener);
        settings.setOnClickListener(listener);
        about.setOnClickListener(listener);
        return view;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (((InfoApplication) getActivity().getApplication()).getAccess_token() == null) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            } else {
                switch (id) {
                    case R.id.profile:
                        startActivity(new Intent(getActivity(), InfoActivity.class));
                        break;
                    case R.id.settings:
                        break;
                    case R.id.about:
                        break;
                }
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        if (info.getAccess_token() != null) {
            tvUsername.setText("个人资料");
            tvIsLogin.setVisibility(View.GONE);
        } else {
            tvUsername.setText("用户名");
            tvIsLogin.setVisibility(View.VISIBLE);
        }
    }
}
